<?php
namespace app\facescore\home;
use app\common\controller\Common;
use think\Db;
class Content extends Common
{
    /**
     * 首页 推荐 列表
     * 如果存在uid，则返回对应uid的发布列表
     * @return string
     */
    public function index()
    {
        $map = [];
        //推荐过的才能上首页
        $map['status'] = 1;
        $map['audit'] = 1;
        //查看主页的时候
        if(input('my_uid') != null) {
            $map['uid'] = input('my_uid');
            unset( $map['status']);
            unset( $map['audit']);
        }
        $map['is_del'] = 0;
        $data_list = Db::name('facescore_content')->where($map)->order('shunxu asc,create_time desc')->paginate(5, false, ['query' => input('get.')])->toArray();
        foreach ($data_list['data'] as $key => $value) {
            //用户昵称
            $data_list['data'][$key]['nick'] = userInfo($data_list['data'][$key]['uid'],'nick');
            //微信头像
            $data_list['data'][$key]['avatar'] = userInfo($data_list['data'][$key]['uid'],'avatar');
            //图集列表
            $data_list['data'][$key]['imgs'] = content_imgs($data_list['data'][$key]['id']);
            //图集处理
            if(input('uid') != null) {
                if(!checkLogin()){
                    return json(['msg' => '没有登录']);
                }
                $data_list['data'][$key]['imgs'] = deal_imgs(input('uid'), $data_list['data'][$key]['imgs'],$data_list['data'][$key]['id']);
            }
            //发布时间
            $data_list['data'][$key]['send_time'] = $this->format_date($data_list['data'][$key]['create_time']);
            //打赏人数
            $data_list['data'][$key]['reward_person_count'] = $this->reward_person_count($data_list['data'][$key]['id']);
        }
       //print_r($data_list);
        return json($data_list);
    }

    /**
     * 首页最新列表
     * @param string $uid
     * @return \think\response\Json
     */
    public function newest()
    {
        $map = [];
        //推荐过的才能上首页

        $map['audit'] = 1;

        $data_list = Db::name('facescore_content')->where($map)->order('create_time desc')->paginate(5, false, ['query' => input('get.')])->toArray();
        foreach ($data_list['data'] as $key => $value) {
            //用户昵称
            $data_list['data'][$key]['nick'] = userInfo($data_list['data'][$key]['uid'],'nick');
            //微信头像
            $data_list['data'][$key]['avatar'] = userInfo($data_list['data'][$key]['uid'],'avatar');
            //图集列表
            $data_list['data'][$key]['imgs'] = content_imgs($data_list['data'][$key]['id']);
            //图集处理
            if(input('uid') != null) {
                if(!checkLogin()){
                    return json(['msg' => '没有登录']);
                }
                $data_list['data'][$key]['imgs'] = deal_imgs(input('uid'), $data_list['data'][$key]['imgs'],$data_list['data'][$key]['id']);
            }
            //发布时间
            $data_list['data'][$key]['send_time'] = $this->format_date($data_list['data'][$key]['create_time']);
            //打赏人数
            $data_list['data'][$key]['reward_person_count'] = $this->reward_person_count($data_list['data'][$key]['id']);
        }
        //print_r($data_list);
        return json($data_list);
    }

    /**
     * 图集图集详情查看
     * @return \think\response\Json
     */
    public function detail($content_id = '')
    {
        $map = [];
        //推荐过的才能上首页

        $map['id'] = $content_id;
        $data_list = Db::name('facescore_content')->where($map)->find();
        //用户昵称
        $data_list['nick'] = userInfo($data_list['uid'],'nick');
        //微信头像
        $data_list['avatar'] = userInfo($data_list['uid'],'avatar');
        //图集列表
        $data_list['imgs'] = content_imgs($data_list['id']);
        //图集处理
        if(input('uid') != null) {
            if(!checkLogin()){
                return json(['msg' => '没有登录']);
            }
            $data_list['imgs'] = deal_imgs(input('uid'),$data_list['imgs'],$content_id);
        }
        //发布时间
        $data_list['send_time'] = $this->format_date($data_list['create_time']);
        //打赏人数
        $data_list['reward_person_count'] = $this->reward_person_count($data_list['id']);

        return json($data_list);
    }
    /**
     * 时间格式转换成 xx 天/月等 前
     * @param $time
     * @return string
     */
    function format_date($time){
        $time = strtotime($time);
        $t=time()-$time;
        $f=array(
            '31536000'=>'年',
            '2592000'=>'个月',
            '604800'=>'星期',
            '86400'=>'天',
            '3600'=>'小时',
            '60'=>'分钟',
            '1'=>'秒'
        );
        foreach ($f as $k=>$v)    {
            if (0 !=$c=floor($t/(int)$k)) {
                return $c.$v.'前';
            }
        }
    }

    /**
     * 打赏总人数
     * @param $content_id
     * @return int|string
     */
    function reward_person_count($content_id){
        $res = Db::name('facescore_reward')->where(['content_id' => $content_id,'status' => 1])->count("distinct uid");
        return $res;
    }

    /**
     * 用户发布内容
     */

}